# Maintainer: Alexey Pavlov <alexpux@gmail.com>
# Maintainer: Ray Donnelly <mingw.android@gmail.com>

_realname=gobject-introspection
pkgbase=mingw-w64-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-${_realname}"
         "${MINGW_PACKAGE_PREFIX}-${_realname}-runtime")
pkgver=1.48.0
pkgrel=2
arch=('any')
url="https://live.gnome.org/GObjectIntrospection"
license=("LGPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cairo"
             "${MINGW_PACKAGE_PREFIX}-glib2"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python2-mako")
# Building this in debug invokes pdb when things go wrong.
# options=('!strip' 'debug')
source=(https://download.gnome.org/sources/gobject-introspection/${pkgver%.*}/${_realname}-${pkgver}.tar.xz
        0001-Revert-Windows-port-Work-arount-MSYS-weirdness-where.patch
        0018-debug-rmtree-errors.mingw.patch
        0021-tests-no-undefined.patch
        0022-change-pkg-config-invocations.mingw.patch
        0024-Support-passing-arguments-to-g-ir-scanner-through-a-.all.patch
        0055-fix-python-detection.patch)

sha256sums=('fa275aaccdbfc91ec0bc9a6fd0562051acdba731e7d584b64a277fec60e75877'
            'f84df99e1ce5a3a0b640ef172de068958eb0b3d0be43a454dd86e6ccb8cfc386'
            '095eec18d93fa2bd936eb11fca54cbd2e3992daf1a0f57cf932a31a06956dee1'
            '663bb39764058b1de1591765f9f465dd69eeae16fcf56b82a3692560849dacae'
            'bb5f48047613216a6c3c652e0ef65c099475ffdddbea4a835b5a51279d7755ac'
            'f4c2c87edf6ac4c993361fa8c5cb95f52fed5a21723148a1bc099e29702fe901'
            'f61d099aa7cd37c437f01d98bb95c57c66f07c78028675ae19fac87f943d189d')

prepare() {
  cd ${srcdir}/${_realname}-${pkgver}
  patch -p1 -i "${srcdir}"/0001-Revert-Windows-port-Work-arount-MSYS-weirdness-where.patch
  patch -p1 -i "${srcdir}"/0018-debug-rmtree-errors.mingw.patch
  patch -p1 -i "${srcdir}"/0021-tests-no-undefined.patch
  patch -p1 -i "${srcdir}"/0022-change-pkg-config-invocations.mingw.patch
  # Source for this patch is:
  # https://bazaar.launchpad.net/~lrn1986/stupidbuild/trunk/view/head:/src/mingw/gobject-introspection-1.0-1.44.0-2/patches/0024-Support-passing-arguments-to-g-ir-scanner-through-a-.all.patch
  patch -p1 -i "${srcdir}"/0024-Support-passing-arguments-to-g-ir-scanner-through-a-.all.patch
  #patch -p1 -i "${srcdir}"/0050-dont-load-msvcrt.patch
  patch -p1 -i "${srcdir}"/0055-fix-python-detection.patch

  autoreconf -fi
}

build() {
  export PYTHON=${MINGW_PREFIX}/bin/python2
  [[ -d "${srcdir}/build-${MINGW_CHOST}" ]] && rm -rf "${srcdir}/build-${MINGW_CHOST}"
  mkdir -p "${srcdir}/build-${MINGW_CHOST}"
  cd "${srcdir}/build-${MINGW_CHOST}"
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --disable-silent-rules

  make
}

package_gobject-introspection() {
  pkgdesc="Introspection system for GObject-based libraries (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-gobject-introspection-runtime=${pkgver}"
           "${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-python2-mako")
  options=('!emptydirs')

  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 DESTDIR="${pkgdir}" install
  make -j1 DESTDIR="${pkgdir}" uninstall-libLTLIBRARIES uninstall-typelibsDATA

  # I've spent too long debugging gobject-introspection and it's too painful each
  # time. Therefore, I've taken the decision to copy the .pyd also as a _d.pyd
  # in-case I happen to be investigating it with a debug version of Python which
  # only loads _d.pyd extensions.
  # You will still have to rebuild this package after having built and installed
  # the debug mingw-w64-python2 of course .. still one less thing to forget to
  # remember.
  # There will be a way to communicate the nature of the Python through to Autoconf
  # and that would be better, but this will have to do for now.
  # (see:
  #  @OS_WIN32_TRUE@	-shrext ".pyd"
  #  in gobject-introspection/Makefile.in)
  PYDS=$(find "${pkgdir}" -name "*.pyd")
  for PYD in "${PYDS}"; do
    pushd $(dirname "${PYD}")
      cp $(basename "${PYD}") $(basename "${PYD}" .pyd)_d.pyd
    popd
  done
}

package_gobject-introspection-runtime() {
  pkgdesc="Introspection system for GObject-based libraries - runtime files (mingw-w64)"
  depends=("${MINGW_PACKAGE_PREFIX}-glib2")

  cd "${srcdir}/build-${MINGW_CHOST}"
  make -j1 DESTDIR="${pkgdir}" install-libLTLIBRARIES install-typelibsDATA
}

# Wrappers
package_mingw-w64-i686-gobject-introspection()
{
  package_gobject-introspection
}

package_mingw-w64-i686-gobject-introspection-runtime()
{
  package_gobject-introspection-runtime
}

package_mingw-w64-x86_64-gobject-introspection()
{
  package_gobject-introspection
}

package_mingw-w64-x86_64-gobject-introspection-runtime()
{
  package_gobject-introspection-runtime
}
